<?php

class Products extends Controller {

	function Products()
	{
		parent::Controller();
		$this->load->helper('admin');
		
		construct_admin_controller($this->EzAuth_Model);
		
		
	}
	
	
	//do something before we load the page:
	//check to see if you are logged in
	
	function _remap($method) {
		checkauth($this, $method, $this->EzAuth_Model);
	}
	

	
	function index()
	{
		$offset = $this->uri->segment(4);
		if (empty($offset)) $offset = 0;
		$data = get_page_ready('products', $offset, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		
		$config['base_url'] = base_url() . 'index.php/admin/products/page/';
		$config['total_rows'] = $data['numitems'];
		$config['uri_segment'] = 4;
		$config['per_page'] = '10';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();
		
		//make the dates pretty
		$this->load->helper('date');
		if (!empty($data['items'])) {
			foreach ($data['items'] as $key =>$item) {
				$newdate = relative_time($item->date);
				$data['items'][$key]->date = $newdate;
			}
		} else {
			$data['items'] = '';
		}
		
		$data['location'] = "products";
		$this->load->view('admin/products', $data);
	}
	
	function page() {
		$this->index();
	}
	
	function process() {
		//delete, make visible or make invisible?
		if(!empty($_POST['button'])) {
			switch  ($_POST['button']) {
				case "Delete":
					if (!empty($_POST['checkall']))
						$this->delete($_POST['checkall']);
					else
						$this->index();
					break;
				case "Make Invisible":
					if (!empty($_POST['checkall']))
						$this->makeinvisible($_POST['checkall']);
					$this->index();
					break;
				case "Make Visible":
					if (!empty($_POST['checkall']))
						$this->makevisible($_POST['checkall']);
					$this->index();
					break;
				default:
					$this->index();
					break;
			}
		} else {
			$this->index();
		}
	}
	
	function delete($items) {
		//items to delete are in $_POST
		
		//are you sure?
		$data = get_page_ready('areyousure',0, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		foreach ($items as $itemindex => $item) {
			//get the title of the item
			$items[$itemindex] = $this->Products_Model->get_title($item);
		}
		
		$data['items'] = $items;
		$data['offset'] = $_POST['offset'];
		$this->load->view('admin/areyousure', $data);
		
	}
	
	function yesiamsure() {
		//really delete the items
		//the items to delete are in an array called $_POST['items']
		$this->Products_Model->delete_item($_POST['items']);
		$this->index();
		
	}
	
	function makeinvisible($items) {
		foreach ($items as $item) {
			//echo "invisibled item number " . $item . "<br />";
			$this->db->or_where('id', $item);
		}
		$this->db->update('items', array('invisible' => 1));
	}
	
	function makevisible($items) {
		foreach ($items as $item) {
			//echo "invisibled item number " . $item . "<br />";
			$this->db->or_where('id', $item);
		}
		$this->db->update('items', array('invisible' => 0));
	}
	
	function search() { 
		$offset = $this->uri->segment(4);
		if (empty($offset)) $offset = 0;
		$data = get_page_ready('search', $offset, $this->Settings_Model, $this->Pages_Model, $this->Products_Model, $this->Categories_Model);
		
		//search
		
		$data['items'] = $this->Products_Model->search($_POST['s'], $_POST['post_status'], $_POST['m'], $_POST['cat']);
		
		/*$config['base_url'] = base_url() . 'index.php/admin/products/search/';
		$config['total_rows'] = count($data['items']);
		$config['uri_segment'] = 4;
		$config['per_page'] = '10';
		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();*/
		
		//make the dates pretty
		$this->load->helper('date');
		if (!empty($data['items'])) {
			foreach ($data['items'] as $key =>$item) {
				$newdate = relative_time($item->date);
				$data['items'][$key]->date = $newdate;
			}
		} else {
			$data['items'] = '';
		}
		
		$data['sticky'] = $_POST;
		
		$data['location'] = "products";
		$this->load->view('admin/products', $data);
	}
	

}
?>